<?php
namespace app\services\weixin\pay;

require_once __DIR__ . "/lib/WxPayData.php";
require_once __DIR__ . "/lib/WxPayNotify.php";
require_once __DIR__ . "/lib/WxPayApi.php";
require_once __DIR__ . '/lanewechat.php';

/**
 * 微信支付回调
 * Created by PhpStorm.
 * User: fanxu(746439274@qq.com)
 * Date: 2015/8/5
 * Time: 15:15
 */


class WeiXinPayCallBack extends \WxPayNotify
{
    /**
     * 查询订单
     * @Author fanxu(746439274@qq.com)
     * @param $transaction_id
     * @return bool
     * @throws \WxPayException
     */
    public function QueryOrder($transaction_id)
    {
        $input = new \WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        $result = \WxPayApi::orderQuery($input);
        if(array_key_exists("return_code", $result)
            && array_key_exists("result_code", $result)
            && $result["return_code"] == "SUCCESS"
            && $result["result_code"] == "SUCCESS")
        {
            return true;
        }
        return false;
    }

    /**
     * 重写回调处理函数
     * @Author fanxu(746439274@qq.com)
     * @param array $data
     * @param string $msg
     * @return bool
     */
    public function NotifyProcess($data, &$msg)
    {
		file_put_contents( 'test.txt' , json_encode( $data ) , FILE_APPEND );
        if(!array_key_exists("transaction_id", $data)){
            $msg = "输入参数不正确";
            return false;
        }
        //查询订单，判断订单真实性
        if(!$this->QueryOrder($data["transaction_id"])){
            $msg = "订单查询失败";
            return false;
        }
        //自己平台订单号
        $orderId = $data['attach'];
		file_put_contents( 'test.txt' , $orderId , FILE_APPEND );
		\LaneWeChat\Core\Common::recharge( $orderId );
        return true;
    }
}